<template lang="pug">
data-page(
  v-bind="pageParams"
)
  mt-table(
    v-bind="tableProps"
  )

  edit-form(
    v-bind="editFormProps"
  )
</template>

<script setup lang="ts">
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type Action,
  type EditFormProps,
  type TableProps,
  useNotify,
} from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { type QueryPagination, useApi } from "@scmt/core-api";
import type {
  EncodingRulesEntity,
  EncodingRulesQueryParams,
} from "@scmt/api-pacs-encoding-rules";

const $api = useApi();
const $notify = useNotify();
const queryPramas = reactive<EncodingRulesQueryParams>({
  name: "",
  pageNum: 1,
  pageSize: 20,
});
const editingObj = ref<null | EncodingRulesEntity>(null);

const { request, data, loadingMap } = useRequest({
  tableData: (q: any) => {
    const params = Object.assign({}, queryPramas, q);
    return $api.pacs.encodingRules.query(params);
  },
  save: (model: EncodingRulesEntity) => {
    return $api.pacs.encodingRules.save(model).then(() => {
      $notify.success("保存成功");
      editingObj.value = null;

      request("tableData", {
        pageSize: 20,
      });
    });
  },
  destroy: (ids: string) => {
    return $api.pacs.encodingRules.destroy(ids).then(() => {
      $notify.success("删除成功");

      request("tableData", {
        pageSize: 20,
      });
    });
  },
});

onMounted(() => {
  request("tableData");
});

const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "numberType",
      label: "编号类型",
    },
    {
      prop: "numberingMethod",
      label: "编号方式",
    },
  ];

  const headerActions: Action[] = ["add"];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    actions: headerActions,
    onQuery: () => request("tableData"),
    onAct: onPageActions,
  };

  return dataPageProps;
});

const tableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    pagination: tableData.value?.pagination,
    columns: [
      {
        type: "index",
        label: "序号",
        width: "60px",
      },

      {
        prop: "numberType",
        label: "编号类型",
        minWidth: "120px",
      },
      {
        prop: "numberingMethod",
        label: "编号方式",
        minWidth: "120px",
      },
      {
        prop: "isPrefix",
        label: "是否前缀",
        minWidth: "120px",
        render: (data) => {
          if (data.row.isPrefix) {
            return "是";
          }
          return "否";
        },
      },
      {
        prop: "numberPrefix",
        label: "编号前缀",
        minWidth: "120px",
      },
      {
        prop: "numberLength",
        label: "编号长度",
        minWidth: "120px",
      },

      {
        prop: "createTime",
        label: "创建时间",
        format: "datetime",
      },
      {
        label: "操作",
        actions: ["edit", "destroy"],
        width: "180px",
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) =>
      request("tableData", pagination),
  };
  return tableProps;
});

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "numberType",
      label: "编号类型",
      required: true,
    },
    {
      prop: "numberingMethod",
      label: "编号方式",
      required: true,
    },
    {
      prop: "isPrefix",
      label: "是否前缀",
      required: true,
      as: "radio-group",
      options: {
        options: [
          {
            label: "是",
            value: 1,
          },
          {
            label: "否",
            value: 0,
          },
        ],
      },
    },
    {
      prop: "numberPrefix",
      label: "编号前缀",
      required: true,
    },
    {
      prop: "numberLength",
      label: "编号长度",
      required: true,
      as: "input-number",
      options: {
        min: 0,
      },
    },
  ];

  const editProps: EditFormProps = {
    model: editingObj.value,
    items: formItems,
    columns: 1,
    size: "mini",
    loading: loadingMap.save,
    onClose: () => {
      editingObj.value = null;
    },
    onCancel: () => {
      editingObj.value = null;
    },
    onValidate: (model: any) => {
      request("save", model);
    },
  };

  return editProps;
});

const onPageActions = (action: IAction, model?: any) => {
  switch (action.event) {
    case "add":
    case "edit":
      editingObj.value = $api.pacs.encodingRules.$$createEntity(model);
      break;
    case "destroy":
      request("destroy", model ? model.id : "");
      break;
  }
};
</script>

<style lang="less" scoped></style>
